//
//  KLSendCarViewController.m
//  KunLun
//
//  Created by Mac on 2017/12/27.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLTobeEspeciallyViewController.h"
#import "STTextView.h"

#define XInsetValeu 20
#define nomalHeight 44
@interface KLTobeEspeciallyViewController ()<KLImageScrollViewDelegate>


@property(nonatomic, strong) UITextField                     *nameTextfiled;
@property(nonatomic, strong) UITextField                     *phoneTextfiled;

//上货地址
@property(nonatomic, strong) STButton                        *addressButton;
@property(nonatomic, strong) UITextField                     *detailAddTextfiled;//详细地址

@property(nonatomic, strong) STTextView                     *destextView;//描述


//banner
@property(nonatomic, strong) KLImageScrollView                     *bannerScrollView;
@property(nonatomic, strong) NSArray                     *imageUrlsArray;
//详细三张
@property(nonatomic, strong) KLImageScrollView                     *detailScrollView;
@property(nonatomic, strong) NSArray                     *detailImageUrlsArray;


@end

@implementation KLTobeEspeciallyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"申请地方特产";
    [self.tableView st_autoAdjustAllResponder];
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self configSubView];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    [self configHeaderView];
    [self configFooterView];
}
- (UIView*)viewformTextField:(UITextField*)textFiled title:(NSString*)title placeHolder:(NSString*)placeHolder{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 89)];
    UIView * shopNameView = [self headerViewWithTitle:title];
    [view addSubview:shopNameView];
    textFiled.frame = CGRectMake(XInsetValeu, shopNameView.bottom, UIScreenWidth - 2*XInsetValeu, 30);
    textFiled.placeholder = placeHolder;
    textFiled.clearButtonMode = UITextFieldViewModeWhileEditing;
    [view addSubview:textFiled];
    
    UIView * line1 = [[UIView alloc] initWithFrame:CGRectMake(0, textFiled.bottom, UIScreenWidth, 0.5)];
    line1.backgroundColor = KL_lineColor;
    [view addSubview:line1];
    view.height = line1.bottom;
    return view;
}
- (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, nomalHeight)];
    backView.backgroundColor = [UIColor whiteColor];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(XInsetValeu, 0, UIScreenWidth - 2* XInsetValeu, nomalHeight)
                                                     text:title
                                                textColor:KL_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [backView addSubview:titleLable];
    return backView;
}
- (void)configHeaderView{
    
    self.nameTextfiled = [UITextField new];
    self.phoneTextfiled = [UITextField new];
    self.detailAddTextfiled = [UITextField new];


    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 150)];
    header.backgroundColor = [UIColor whiteColor];
    
    
    UIView * biaotiView = [self viewformTextField:self.nameTextfiled
                                            title:@"姓名"
                                      placeHolder:@"至少2个字"];
    [header addSubview:biaotiView];
    
    
    //shoujihao
    UIView * phoneView = [self viewformTextField:self.phoneTextfiled
                                           title:@"联系电话"
                                     placeHolder:@"请输入正确的手机号"];
    phoneView.top = biaotiView.bottom;
    self.phoneTextfiled.keyboardType = UIKeyboardTypeNumberPad;
    [header addSubview:phoneView];

    __weak typeof(self) weakSelf =  self;
    STButton * ruleButton = [[STButton alloc] initWithFrame:CGRectMake(XInsetValeu, phoneView.bottom, UIScreenWidth - 20, 40)
                                                      title:@"平台服务规则"
                                                 titleColor:KL_firstTextColor
                                                  titleFont:15
                                               cornerRadius:0
                                            backgroundColor:[UIColor whiteColor]
                                            backgroundImage:nil
                                                      image:[UIImage imageNamed:@"问号"]];
    [ruleButton makeImageRight];
    [ruleButton setClicAction:^(UIButton *sender) {
        [weakSelf onsSelectedRultButton];
    }];
    [header addSubview:ruleButton];
    
    //经营地址
    self.addressButton = [[STButton alloc] initWithFrame:CGRectMake(XInsetValeu, ruleButton.bottom, UIScreenWidth - 2*XInsetValeu, 40)
                                                   title:@"点击修改地址"
                                              titleColor:KL_firstTextColor
                                               titleFont:15
                                            cornerRadius:0
                                         backgroundColor:nil
                                         backgroundImage:nil
                                                   image:nil];
    [self.addressButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedAddressButton];
    }];
    [header addSubview:self.addressButton];
    
    //详细地址
    UIView * detailAdressView = [self viewformTextField:self.detailAddTextfiled
                                                  title:@"详细地址"
                                            placeHolder:@"请输入详细地址"];
    detailAdressView.top = self.addressButton.bottom;
    [header addSubview:detailAdressView];
    
  
  
    
    UIView * goodDetailView = [self headerViewWithTitle:@"特产简介"];
    goodDetailView.top = detailAdressView.bottom;
    [header addSubview:goodDetailView];
    self.destextView = [[STTextView alloc] initWithFrame:CGRectMake(XInsetValeu, goodDetailView.bottom, UIScreenWidth - 2*XInsetValeu, 80)];
    self.destextView.placeholder = @"请输入特产详情描述,至少20个字";
    [self.destextView st_setBorderWith:1 borderColor:KL_lineColor cornerRadius:5];
    [header addSubview:self.destextView];
    
    header.height = _destextView.bottom;
    self.tableView.tableHeaderView = header;
}
- (void)configFooterView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = [UIColor whiteColor];
    
    //封面
    self.bannerScrollView = [[KLImageScrollView alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth - 30, 120)];
    self.bannerScrollView.title = @"上传封面图片:(提示，至少上传三张图片)";
    self.bannerScrollView.delegate = self;
    self.imageUrlsArray = self.bannerScrollView.urlArray;
    [footer addSubview:self.bannerScrollView];
    //封面
    
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, self.bannerScrollView.bottom + 30, UIScreenWidth - 60, 44)
                                                        title:@"提交"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:KL_BlueBackGroundColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    
    footer.height = confimButton.bottom + 20;
    self.tableView.tableFooterView = footer;
}
#pragma mark --KLImageScrollViewDelegate
- (void)klimageScrollView:(KLImageScrollView *)imageScrollView didSendImageUrls:(NSArray *)array{
    [self onSelectedCommitButton];
}
#pragma mark --Action Method
//规则
- (void)onsSelectedRultButton{
    
}
- (void)onSelectedAddressButton{
    
    STPickerViewController * vc = [[STPickerViewController alloc] initWithDefualtAreaPickerWithHandle:^(NSString *finshString, NSString *area, NSString *city, NSString *provence) {
        [self.addressButton setTitle:[NSString stringWithFormat:@"%@%@",provence,city] forState:UIControlStateNormal];
    }];
    [self presentViewController:vc animated:NO completion:nil];
}

- (void)onSelectedCommitButton{
    if (self.nameTextfiled.text.length < 2) {
        [SVProgressHUD showErrorWithStatus:@"姓名最少2个字"];
        return;
    }
    if (!self.phoneTextfiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号"];
        return;
    }
    if ([self.addressButton.currentTitle isEqualToString:@"点击修改地址"]) {
        [SVProgressHUD showErrorWithStatus:@"请选择上地址"];
        return;
    }
    if (!self.detailAddTextfiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入详细上货地址"];
        return;
    }

    if (!self.destextView.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入特产描述"];
        return;
    }
    
    if (self.bannerScrollView.dataSouce.count < 3 ) {
        [SVProgressHUD showErrorWithStatus:@"封面图片最少三张"];
        return;
    }
    
    [SVProgressHUD showWithStatus:@"请稍后"];
    if (self.bannerScrollView.isSendAll) {
        self.imageUrlsArray = self.bannerScrollView.urlArray;
        [self sendGoodInfoRequest];
    
        
    }else{
        if (!self.bannerScrollView.isSendAll) {
            [self.bannerScrollView sendImageToQiNiu];
        }

    }
}
- (void)sendGoodInfoRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/shop/applyspecialty",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    
    if (self.nameTextfiled.text.length) {
        [dic setObject:self.nameTextfiled.text forKey:@"name"];
    }
    //地址
    if (self.addressButton.currentTitle.length && self.detailAddTextfiled.text.length) {
        NSString * stri = [NSString stringWithFormat:@"%@%@",self.addressButton.currentTitle,self.detailAddTextfiled.text];
        [dic setObject:stri forKey:@"address"];
    }
    
    

    //详情
    if (self.destextView.text.length) {
        [dic setObject:self.destextView.text forKey:@"abstract"];
    }
    
    //phone
    if (self.phoneTextfiled.text.length) {
        [dic setObject:self.phoneTextfiled.text forKey:@"phone"];
    }
    if (self.shopId.length) {
        [dic setObject:self.shopId forKey:@"shop_id"];
    }
    //图片
    NSString * cover_map = [self.imageUrlsArray componentsJoinedByString:@","];
    [dic setObject:cover_map forKey:@"pictures"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"已收到你的申请"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 
                                                 
                                             }];
}
@end

